package com.cashmama.market.api.domain.utils;

import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.consts.RedisKeyConsts;
import com.cashmama.market.api.infras.consts.TimeConsts;
import com.cashmama.market.api.infras.enums.RestCodeEnum;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.qianli.cashmama.product.facade.AppFacade;
import com.qianli.cashmama.product.facade.result.AppConfig;
import com.qianli.cashmama.product.facade.result.AppVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 马甲包工具类
 *
 * @author moss
 * 2019-04-02
 */
@Component
public class AppUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppUtils.class);

    /**
     * cashmama初期code使用的是旧编码 对应新版本JYD编码
     */
    public static final String OLD_CASHMAMA_CODE = "0000002";

    public static final String NEW_CASHMAMA_CODE = "5";

    @Autowired
    ApolloConfigUtil apolloConfigUtil;

    @Autowired
    private AppFacade appFacade;

    @Autowired
    private RedisClient redisClient;

    public AppConfig getAppConfig() {
        String appCode = RequestLocalInfo.getRequestBasicInfo().getAppCode();
        try {
            return getAppConfig(appCode);
        } catch (ApplicationException e) {
            LOGGER.info("查询app信息失败,appCode:{}", appCode, e);
            throw e;
        } catch (Exception e) {
            LOGGER.info("查询app信息异常,appCode:{}", appCode, e);
            throw new ApplicationException(RestCodeEnum.QUERY_FAILED);
        }
    }

    /**
     * 查询APP信息
     *
     * @return
     */
    public AppVo getAppVo() {
        try {
            String appCode = RequestLocalInfo.getRequestBasicInfo().getAppCode();
            AppVo appVo = appFacade.getAppByAppCode(appCode).getData();
            return appVo;
        } catch (Exception e) {
            LOGGER.info("查询app信息异常", e);
            return null;
        }
    }

    public AppVo getAppByAppCode(String appCode) {
        try {
            AppVo appVoCache = redisClient.get(RedisKeyConsts.APPVO_CACHA, appCode);
            if (appVoCache != null) {
                return appVoCache;
            }
        } catch (Exception e) {
            LOGGER.error("查询app缓存信息失败,appCode:{}", appCode, e);
        }
        Response<AppVo> appVoResponse = appFacade.getAppByAppCode(appCode);
        if (!appVoResponse.isSuccess() || appVoResponse.getData() == null) {
            throw new ApplicationException("Query failed");
        }
        AppVo appVo = appVoResponse.getData();
        redisClient.set(RedisKeyConsts.APPVO_CACHA, appVo, TimeConsts.TEN_MINUTES, appCode);
        return appVo;
    }

    public AppConfig getAppConfig(String appCode) {
        return getAppByAppCode(appCode).getConfig();
    }

    /**
     * appCode
     *
     * @return
     */
    public String getAppCode() {
        return this.getAppVo().getAppCode();
    }

    /**
     * 注册协议
     *
     * @return
     */
    public String getRegisterProtocol(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.register.protocol", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    /**
     * 隐私协议
     */
    public String getPrivacyPolicyUrl(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.privacy.policy.url", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    public String getTermsOfServiceUrl(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.terms.of.services.url", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    /**
     * 关于我们
     */
    public String getUserAboutUrl(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.aboutURL", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    /**
     * 联系我们
     */
    public String getContactUs(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.contactus", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    /**
     * 常见问题
     */
    public String getFAQS(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.faqs", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    public String getContactUsIcon(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.contactus.icon", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    public String getUserAboutUrlIcon(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.aboutURL.icon", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    public String getFAQSIcon(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.faqs.icon", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    public String getNoticeIcon(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.notice.icon", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    public String getLogoutIcon(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.user.logout.icon", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getString(key);
        }
        return "";
    }

    /**
     * Android版 小于此版本都需要升级
     */
    public int getAndroidMinVersionCode(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.forceUpdate.androidMinVersionCode", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getInteger(key);
        }
        return 1;
    }

    /**
     * 是否强制升级
     */
    public boolean isForceUpdate(String packageName, JSONObject jsonObject) {
        String key = String.format("%s.force.update", packageName);
        if(jsonObject != null && jsonObject.containsKey(key)){
            return jsonObject.getBoolean(key);
        }
        return false;
    }

    /**
     * 商店 app预览地址
     */
    public String getStoreAppReviewUrl() {
        return "";
    }

    /**
     * 需要完整的短信和通话记录
     */
    public boolean needCompleteSmsAndCallHistory() {
        return true;
    }

    /**
     * 获取底部tab列表
     *
     * @param packageName
     * @return
     */
    public String getMenuListUrl(String packageName) {
        return apolloConfigUtil.getMenuList();
    }
}
